home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / TUTORIAL.BIN / NetData.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-01-30  |  4.4 KB  |  190 lines

  1. package symantec.itools.db.net;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.DataOutputStream;
  5. import java.io.EOFException;
  6. import java.io.IOException;
  7. import symjava.sql.SQLException;
  8.  
  9. public class NetData extends ServerObject {
  10.    int _length;
  11.    byte[] _data;
  12.  
  13.    public NetData(byte[] data) {
  14.       this._data = data;
  15.       this._length = data.length;
  16.    }
  17.  
  18.    public NetData(byte[] data, int len) {
  19.       this._data = data;
  20.       this._length = len;
  21.    }
  22.  
  23.    public NetData(byte data) {
  24.       this._length = 1;
  25.       this._data = new byte[this._length];
  26.       this._data[0] = data;
  27.    }
  28.  
  29.    public NetData(short data) {
  30.       this._length = 2;
  31.       this._data = new byte[this._length];
  32.       this._data[0] = (byte)(data >>> 8 & 255);
  33.       this._data[1] = (byte)(data & 255);
  34.    }
  35.  
  36.    public NetData(boolean data) {
  37.       this._length = 1;
  38.       this._data = new byte[this._length];
  39.       this._data[0] = (byte)(data ? 1 : 0);
  40.    }
  41.  
  42.    public NetData(int data) {
  43.       this._length = 4;
  44.       this._data = new byte[this._length];
  45.       this._data[0] = (byte)(data >>> 24 & 255);
  46.       this._data[1] = (byte)(data >>> 16 & 255);
  47.       this._data[2] = (byte)(data >>> 8 & 255);
  48.       this._data[3] = (byte)(data & 255);
  49.    }
  50.  
  51.    public NetData(long data) {
  52.       this._length = 8;
  53.       this._data = new byte[this._length];
  54.       this._data[0] = (byte)((int)(data >>> 56) & 255);
  55.       this._data[1] = (byte)((int)(data >>> 48) & 255);
  56.       this._data[2] = (byte)((int)(data >>> 40) & 255);
  57.       this._data[3] = (byte)((int)(data >>> 32) & 255);
  58.       this._data[4] = (byte)((int)(data >>> 24) & 255);
  59.       this._data[5] = (byte)((int)(data >>> 16) & 255);
  60.       this._data[6] = (byte)((int)(data >>> 8) & 255);
  61.       this._data[7] = (byte)((int)data & 255);
  62.    }
  63.  
  64.    public NetData(float data) {
  65.       this._length = 4;
  66.       this._data = new byte[this._length];
  67.       int i = Float.floatToIntBits(data);
  68.       this._data[0] = (byte)(i >>> 24 & 255);
  69.       this._data[1] = (byte)(i >>> 16 & 255);
  70.       this._data[2] = (byte)(i >>> 8 & 255);
  71.       this._data[3] = (byte)(i & 255);
  72.    }
  73.  
  74.    public NetData(double data) {
  75.       this._length = 8;
  76.       this._data = new byte[this._length];
  77.       long l = Double.doubleToLongBits(data);
  78.       this._data[0] = (byte)((int)(l >>> 56) & 255);
  79.       this._data[1] = (byte)((int)(l >>> 48) & 255);
  80.       this._data[2] = (byte)((int)(l >>> 40) & 255);
  81.       this._data[3] = (byte)((int)(l >>> 32) & 255);
  82.       this._data[4] = (byte)((int)(l >>> 24) & 255);
  83.       this._data[5] = (byte)((int)(l >>> 16) & 255);
  84.       this._data[6] = (byte)((int)(l >>> 8) & 255);
  85.       this._data[7] = (byte)((int)l & 255);
  86.    }
  87.  
  88.    public NetData(String data) {
  89.       this._length = data.length();
  90.       this._data = new byte[this._length];
  91.  
  92.       for(int i = 0; i < this._length; ++i) {
  93.          this._data[i] = (byte)data.charAt(i);
  94.       }
  95.  
  96.    }
  97.  
  98.    public NetData() {
  99.       this._length = 0;
  100.       this._data = new byte[this._length];
  101.    }
  102.  
  103.    int getType() {
  104.       return 51;
  105.    }
  106.  
  107.    void read(DataInputStream in) throws SQLException, IOException, ErrorException {
  108.       this._length = in.readShort();
  109.       if (this._length > 0) {
  110.          this._data = new byte[this._length];
  111.          in.readFully(this._data, 0, this._length);
  112.       }
  113.  
  114.    }
  115.  
  116.    void write(DataOutputStream out) throws IOException {
  117.       out.writeByte(this.getType());
  118.       out.writeShort(this._length);
  119.       if (this._length > 0) {
  120.          out.write(this._data, 0, this._length);
  121.       }
  122.  
  123.    }
  124.  
  125.    public int getInt() throws EOFException {
  126.       if (this._length >= 4) {
  127.          int b1 = this._data[0] & 255;
  128.          int b2 = this._data[1] & 255;
  129.          int b3 = this._data[2] & 255;
  130.          int b4 = this._data[3] & 255;
  131.          return (b1 << 24) + (b2 << 16) + (b3 << 8) + b4;
  132.       } else {
  133.          throw new EOFException();
  134.       }
  135.    }
  136.  
  137.    public short getShort() throws EOFException {
  138.       if (this._length >= 2) {
  139.          int i1 = this._data[0] & 255;
  140.          int i2 = this._data[1] & 255;
  141.          return (short)((i1 << 8) + i2);
  142.       } else {
  143.          throw new EOFException();
  144.       }
  145.    }
  146.  
  147.    public boolean getBool() throws EOFException {
  148.       if (this._length >= 1) {
  149.          int i = this._data[0];
  150.          return i != 0;
  151.       } else {
  152.          throw new EOFException();
  153.       }
  154.    }
  155.  
  156.    public byte getByte() throws EOFException {
  157.       if (this._length >= 1) {
  158.          return (byte)(this._data[0] & 255);
  159.       } else {
  160.          throw new EOFException();
  161.       }
  162.    }
  163.  
  164.    public long getLong() throws EOFException {
  165.       if (this._length >= 8) {
  166.          int i1 = this.getInt();
  167.          int b1 = this._data[4] & 255;
  168.          int b2 = this._data[5] & 255;
  169.          int b3 = this._data[6] & 255;
  170.          int b4 = this._data[7] & 255;
  171.          int i2 = (b1 << 24) + (b2 << 16) + (b3 << 8) + b4;
  172.          return ((long)i1 << 32) + ((long)i2 & 4294967295L);
  173.       } else {
  174.          throw new EOFException();
  175.       }
  176.    }
  177.  
  178.    public float getFloat() throws EOFException {
  179.       return Float.intBitsToFloat(this.getInt());
  180.    }
  181.  
  182.    public double getDouble() throws EOFException {
  183.       return Double.longBitsToDouble(this.getLong());
  184.    }
  185.  
  186.    public byte[] getBytes() {
  187.       return this._data;
  188.    }
  189. }
  190.